﻿///disablekeys.cs is an adapted mechanism derived from online code snippets
///to disable many key combos that are undesirable for users to trigger
///whilst the application is being run.
///


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace mainAppView
{
    public class disablekeys
    {

        /* Code to Disable WinKey, Alt+Tab, Ctrl+Esc Starts Here */


        // Structure contain information about low-level keyboard input event 
        [StructLayout(LayoutKind.Sequential)]
        public struct KBDLLHOOKSTRUCT
        {
            public Keys key;
            public int scanCode;
            public int flags;
            public int time;
            public IntPtr extra;
        }


        //System level functions to be used for hook and unhook keyboard input  
        public delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool UnhookWindowsHookEx(IntPtr hook);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp);
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr GetModuleHandle(string name);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern short GetAsyncKeyState(Keys key);
        //Declaring Global objects     
        public IntPtr ptrHook;
        public LowLevelKeyboardProc objKeyboardProcess;

        public IntPtr captureKey(int nCode, IntPtr wp, IntPtr lp)
        {
            if (nCode >= 0)
            {
                KBDLLHOOKSTRUCT objKeyInfo = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lp, typeof(KBDLLHOOKSTRUCT));

                // Disabling Windows keys 

                if (objKeyInfo.key == Keys.Space && HasAltModifier(objKeyInfo.flags) || objKeyInfo.key == Keys.Control || objKeyInfo.key == Keys.Alt || objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin || objKeyInfo.key == Keys.Tab && HasAltModifier(objKeyInfo.flags) || objKeyInfo.key == Keys.F4 || objKeyInfo.key == Keys.Escape || objKeyInfo.key == Keys.Delete)
                {
                    return (IntPtr)1; // if 0 is returned then All the above keys will be enabled
                }

                if (objKeyInfo.key == Keys.F1)
                {
                    Application.Exit();
                }

            }
            return CallNextHookEx(ptrHook, nCode, wp, lp);
        }

        bool HasAltModifier(int flags)
        {
            return (flags & 0x20) == 0x20;
        }

        /* Code to Disable WinKey, Alt+Tab, Ctrl+Esc Ends Here */
    }
}